package com.car.util;

import java.io.UnsupportedEncodingException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.codec.DecoderException;
import org.apache.commons.codec.binary.Hex;
import org.json.JSONException;




public class Test {
	/**
	 * 判断字符是否是中文
     *
     * @param c 字符
     * @return 是否是中文
     */
    public static boolean isChinese(char c) {
        Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
        if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
                || ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
                || ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
                || ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
                || ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
            return true;
        }
        return false;
    }
	
    /**
     * 判断字符串是否是乱码
     *
     * @param strName 字符串
     * @return 是否是乱码
     */
	public static boolean isMessyCode(String strName) {
        Pattern p = Pattern.compile("\\s*|t*|r*|n*");
        Matcher m = p.matcher(strName);
        String after = m.replaceAll("");
        String temp = after.replaceAll("\\p{P}", "");
        char[] ch = temp.trim().toCharArray();
        float chLength = ch.length;
        float count = 0;
        for (int i = 0; i < ch.length; i++) {
            char c = ch[i];
            if (!Character.isLetterOrDigit(c)) {
                if (!isChinese(c)) {
                    count = count + 1;
                }
            }
        }
        float result = count / chLength;
        if (result > 0.4) {
            return true;
        } else {
            return false;
        }
 
    }
	
	public static String toHex(byte[] data){
        String ret = new String();

        if (data != null && data.length>0) {
            StringBuilder sb = new StringBuilder();
            for (byte b: data){
                //分别获取高四位，低四位的内容，将两个数值，转为字符
                int h = (b>>4)&0x0f;
                int l = b&0x0f;
                char ch ,cl;
                if( h > 9 ){
                    ch = (char) ('a'+(h-10));
                }else{  //0--9
                    ch = (char) ('0'+h);
                }

                if(l>9){
                    cl = (char) ('a'+(l-10));
                }else{  //0--9
                    cl = (char) ('0'+l);
                }

                    sb.append(ch).append(cl);
                }
                  ret = new String(sb);
				try {
					/* byte[] b2 = ret.getBytes("utf-8");
					 ret=new String(b2,"utf-8");*/
					 System.out.println("ret:"+ret);
				} catch (Exception e) {
					e.printStackTrace();
				}
      			
            }

        return ret;
    }
	
	public static byte[] hexDecode(String input) {
		try {
			return Hex.decodeHex(input.toCharArray());
		} catch (DecoderException e) {
			throw new IllegalStateException("Hex Decoder exception", e);
		}
	}
	public static boolean cclm(String source) {
		boolean flag = true;
		char ws[] = new char[] { '"', '?', ' ', '\'', '&' };
		for (int i = 0; i < source.length(); i++) {
			char c = source.charAt(i);
			for (int j = 0; j < ws.length; j++) {
				char v = ws[j];
				if (c == v) {
					flag = false;
				}
			}
			if ((int) c == 0xfffd) {
				flag = false;
			}
		}
		return flag;
	}
	
	public static boolean getHanzi(String str) {
		boolean flag=false;
		int count = 0; 
	    String regEx = "[\\u4e00-\\u9fa5]"; 
	    Pattern p = Pattern.compile(regEx); 
	    Matcher m = p.matcher(str); 
	    while (m.find()) { 
	    	for (int i = 0; i <= m.groupCount(); i++) { 
	    		count = count + 1; 
	    	} 
	    } 
	    if(count>0){
	    	flag=true;
	    }
		return flag;
	}
	
	public static void main(String[] args) throws Exception, JSONException {
		Test test = new Test();
		byte[] a = "421424wqewqew卧槽aav1313[][]{}".getBytes();
		new String(a, "UTF-8");
		System.out.println(test.toHex(a));
		
		byte[] bytes=hexDecode(test.toHex(a));
		
		String s = new String(bytes, "utf-8");
		System.out.println("解密："+s);
		
		System.out.println(isMessyCode(s));
	    System.out.println(isMessyCode("你好"));
	    System.out.println(cclm(s));
	    
	    System.out.println("是不是汉字："+getHanzi("e5819ae4ba8be6af9be8ba81efbc8ce5a5bde58aa8，"));
	    
	}

}

